package cn.danger.com.util;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.AppCompatCheckBox;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.LinearLayout;

/**
 * Created by 俊 on 2017/1/17.
 */
public class AnimatorUtil {

    public static void setVisibleAnimator(final FloatingActionButton delete_button) {
        if (delete_button.getVisibility() == View.GONE) {
            ObjectAnimator visible_animator = ObjectAnimator.ofFloat(delete_button, "alpha", 0f, 1.0f);
            visible_animator.setDuration(500);
            visible_animator.setInterpolator(new AccelerateInterpolator());
            visible_animator.start();
            delete_button.setVisibility(View.VISIBLE);
        }
    }

    public static void setGoneAnimator(final FloatingActionButton delete_button) {
        if (delete_button.getVisibility() == View.VISIBLE) {
            ObjectAnimator gone_animator = ObjectAnimator.ofFloat(delete_button, "alpha", 1.0f, 0f);
            gone_animator.setDuration(500);
            gone_animator.setInterpolator(new AccelerateInterpolator());
            gone_animator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    delete_button.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            gone_animator.start();
        }
    }

    public static void setVisibleAnimator(AppCompatCheckBox check_box, LinearLayout body_layout, Context context) {
        AnimatorSet set = new AnimatorSet();
//        ObjectAnimator check_box_visible_animator = ObjectAnimator.ofFloat(check_box, "alpha", 0f, 1.0f);
        ObjectAnimator check_box_translate_animator = ObjectAnimator.ofFloat(check_box, "translateX", 0f, DensityUtil.dip2px(context, 32));
        ObjectAnimator body_layout_animator = ObjectAnimator.ofFloat(body_layout, "translateX", 0f, DensityUtil.dip2px(context, 32));
        set.playTogether(check_box_translate_animator, body_layout_animator);
        set.setDuration(500);
        set.setInterpolator(new AccelerateInterpolator());
        set.start();
        check_box.setVisibility(View.VISIBLE);
    }

    public static void setGoneAnimator(final AppCompatCheckBox check_box, LinearLayout body_layout, Context context) {
        AnimatorSet set = new AnimatorSet();
//        ObjectAnimator check_box_visible_animator = ObjectAnimator.ofFloat(check_box, "alpha", 1.0f, 0f);
        ObjectAnimator check_box_translate_animator = ObjectAnimator.ofFloat(check_box, "translateX", DensityUtil.dip2px(context, 32), 0f);
        ObjectAnimator body_layout_animator = ObjectAnimator.ofFloat(body_layout, "translateX", DensityUtil.dip2px(context, 32), 0f);
        set.playTogether(check_box_translate_animator, body_layout_animator);
        set.setDuration(500);
        set.setInterpolator(new AccelerateInterpolator());
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                check_box.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        set.start();
    }
}
